#include <Ogre.h>

#include "IntroState.h"
#include "NetworkState.h"

using namespace Ogre;

IntroState IntroState::mIntroState;

IntroState::IntroState()
{
	mWindow = 0;
	mDebugOverlay = 0;
	displayCameraDetails = false;
	statsOn = false;
	mTimeUntilNextToggle = 0.0f;
}

void IntroState::enter()
{
	mRoot = Root::getSingletonPtr();
	mWindow = mRoot->getAutoCreatedWindow();
	mDeviceManager = DeviceManager::getSingletonPtr();
	mGUIManager = GUIManager::getSingletonPtr();
	mKeyboard = mDeviceManager->mKeyboard;
	mMouse = mDeviceManager->mMouse;
	mDeviceManager->setBuffered(true);

	// Set up Debug Overlay
	mDebugOverlay = OverlayManager::getSingleton().getByName("Core/DebugOverlay");

	// Create SceneManager	
	mSceneMgr = mRoot->getSceneManager("plitzScene");

	// Create Camera
	mCamera = mSceneMgr->createCamera("IntroCamera");

	// Create Viewport
	mViewport = mRoot->getAutoCreatedWindow()->addViewport(mCamera);
	mViewport->setBackgroundColour(ColourValue(0.0, 0.0, 0.0));

	// Create Sprite Manager
	spriteManager=new Ogre2dManager;
	spriteManager->init(mSceneMgr, Ogre::RENDER_QUEUE_OVERLAY, true);

	// Reigster GUI Items
	mGUIManager->registerScene(mSceneMgr);
	mGUIManager->setGUISheet(CEGUI::WindowManager::getSingletonPtr()->getWindow("Intro/Sheet"));
	setupEventHandlers();

	// Set default values for each entry
	mExitGame = false;
	mGoToNet = false;
	
}

void IntroState::exit()
{
	// Clean up sprite manager
	spriteManager->end();
	delete spriteManager;

	// Switch off debug overlays
	displayCameraDetails = false;
	statsOn = false;
	mDebugOverlay->hide();

	// Clean up scene
	mSceneMgr->clearScene();
	mSceneMgr->destroyAllCameras();
	mRoot->getAutoCreatedWindow()->removeAllViewports();
}

void IntroState::pause()
{
}

void IntroState::resume()
{
}

bool IntroState::keyPressed(const OIS::KeyEvent &arg)
{
	if (arg.key == OIS::KC_P)	// Text says play but goes to NETSTATE not PLAYSTATE
		mGoToNet = true;
	if (arg.key == OIS::KC_Q || arg.key == OIS::KC_ESCAPE)
		mExitGame = true;

	// Toggle Debug Overlay
	if (arg.key == OIS::KC_F1 && mTimeUntilNextToggle <= 0 && Config::getInstance()->cfg_debugOn)
	{
		statsOn = !statsOn;
		mTimeUntilNextToggle = 0.5;
		if (statsOn) mDebugOverlay->show();
		else mDebugOverlay->hide();
	}

	return true;
}

bool IntroState::keyReleased(const OIS::KeyEvent &arg)
{
	return true;
}

bool IntroState::mouseMoved( const OIS::MouseEvent &arg )
{
	CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel );
	return true;
}

bool IntroState::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
	CEGUI::System::getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id));
	return true;
}

bool IntroState::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
	CEGUI::System::getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id));
	return true;
}

bool IntroState::frameStarted(const FrameEvent& evt)
{
	// Update toggle buffering time
	if (mTimeUntilNextToggle >= 0)
				mTimeUntilNextToggle -= evt.timeSinceLastFrame;

	// Updating debug overlay stats
	updateStats();

	drawSprites();

	if (mKeyboard) 
		mKeyboard->capture();
	if (mMouse)
		mMouse->capture();
	return true;
}

bool IntroState::frameEnded(const FrameEvent& evt)
{
	if (mExitGame)
		return false;
	if (mGoToNet)
		changeState(NetworkState::getInstance());
	return true;
}

void IntroState::createScene()
{
}

void IntroState::setupEventHandlers()
{
	CEGUI::WindowManager *wmgr = CEGUI::WindowManager::getSingletonPtr();
	CEGUI::Window *play = wmgr->getWindow((CEGUI::utf8*)"Intro/PlayButton");
	play->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&IntroState::handlePlay, this));
    CEGUI::Window *quit = wmgr->getWindow((CEGUI::utf8*)"Intro/QuitButton");
	quit->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&IntroState::handleQuit, this));
}

bool IntroState::handleQuit(const CEGUI::EventArgs& e)
{
	mExitGame = true;
	return true;
}

bool IntroState::handlePlay(const CEGUI::EventArgs& e)
{
	mGoToNet = true;
	return true;
}

void IntroState::drawSprites()
{
	//spriteManager->drawSprite("title.png", 320,140,700,440);
	spriteManager->drawSprite("title2.png", 200,160,820,460);
	//spriteManager->spriteBltFull("title.png", -0.375, 0.347826087, 0.3671875, 0.02173913);
}